﻿<?php 
//error_reporting(0);
include 'excel_reader.php';
include 'conn.php';
include 'common.php';?>
<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>导入用户</title>
<body>
<div class="container">
<div class="nav_header">
<?php include 'head.php';?>
</div>
<div class="main_cj_content">	

<p class="welcome">导入用户：</p>
<hr>
<br />
<br />



<?php

if($_FILES["user_file"])
{
	$filename=$_FILES["user_file"]["name"];

if($_FILES['user_file']["error"]>0)
{
	echo "<script>alert('错误: " . $_FILES["user_file"]["error"] . "');</script>";
	header("Location:admin.php");
}
else if(!strpos($filename,"xls")||(strpos($filename,"xls")!=strlen($filename)-3))
{
	echo "<script>alert('错误:应该上传.xls后缀的excel文件');</script>";
	header("Location:admin.php");
}
else 
{
	$i=1;
	$save_filename="files/users".date("Y-m-d[").$i."].xls";
	 while(file_exists("files/users".date("Y-m-d[").$i."].xls"))
 {
 	$i=$i+1;
   	$save_filename="files/users".date("Y-m-d[").$i."].xls";
  }
move_uploaded_file($_FILES["user_file"]["tmp_name"],
	$save_filename);
	
	sleep(2);
	$excel_data=new Spreadsheet_Excel_Reader($save_filename);
	echo "<p>上传文件成功!上传的文件存储在: " . $save_filename."</p>";
	//echo $excel_data->dump();
	$rowcount=$excel_data->rowcount();
	mysql_query("BEGIN");
	for($i=2;$i<=$rowcount;$i++)
	{
		$realname=$excel_data->val($i, 1);
		$username=$excel_data->val($i, 2);
		$user_category=$excel_data->val(($i), 3);
		$user_class=$excel_data->val($i, 4);
		$exe="insert into users(username,passwd,user_category,realname,class) values('$username','".md5("111111")."',$user_category,'$realname','$user_category')";
		$rs=mysql_query($exe);
		if(!$rs)
		{
			mysql_query("ROLLBACK");
			echo "<script language=javascript>alert('导入失败!请检查上传文件格式后重新上传')</script>";
			unlink($save_filename);
			exit();
		}
	}
	mysql_query("COMMIT");
	echo "<p>导入成功!已导入".($rowcount-1)."条记录！</p>";
}
}


if($_POST['username'])
{
	$username=getpost('username');
	
}
?>
<?php include 'foot.php';?>
</div>
</div>
</body>
</html>
